/*
 * Alloy Analyzer
 * Copyright (c) 2002 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package alloy.transform;

import java.util.Set;
import java.util.TreeSet;

import alloy.ast.ASTDepthFirstVisitor;
import alloy.ast.ComprehensionExpr;
import alloy.ast.DeclIter;
import alloy.ast.Id;
import alloy.ast.Node;
import alloy.ast.QualifiedName;
import alloy.ast.QuantifiedFormula;
import alloy.ast.SumIntExpr;
import alloy.ast.TypedExpr;
import alloy.ast.VarCreator;
import alloy.ast.Variable;
import alloy.ast.VariableExpr;

/**
 * A visitor used by {@link SkolemizationVisitor} to ensure that any skolem constants created
 * by that visitor have globally unique names.  Gathers all the names with which skolem constant
 * names must not conflict: relation names, quantified variable names; other?
 * <p>
 * Implementation note: right now some of the name-adding code might be extraneous/redundant -- the same
 * name might be added several times.   This should be harmless as we're adding to a set.
 */
class GatherNamesVisitor extends ASTDepthFirstVisitor {
    /**
     * The set of gathered names.
     */
    private Set /* of String */ _names = new TreeSet();

    //////////////
    // Methods //
    ////////////

    /** Save the names of variables in a VarCreator */
    private void _visitVarCreator(VarCreator vc_) {
	for (DeclIter declIter = new DeclIter(vc_); declIter.hasNext();) {
	    declIter.next();
	    _names.add(declIter.getVariable().getId().nodeString());
	}
    }

    /** Return the gathered names */
    public Set /* of String */ getNames() { return _names; }

    /** Visit a node of type {@link QuantifiedFormula}
     *  and traverse its children depth first*/
    public void visit(QuantifiedFormula quantifiedFormula_){
	_visitVarCreator(quantifiedFormula_);
	super.visit(quantifiedFormula_);
    }

    /** Visit a node of type {@link ComprehensionExpr}
     *  and traverse its children depth first*/
    public void visit(ComprehensionExpr comprehensionExpr_){
	_visitVarCreator(comprehensionExpr_);
        visit((TypedExpr)comprehensionExpr_);
    }
    
    /** Visit a node of type {@link SumIntExpr}
     *  and traverse its children depth first*/
    public void visit(SumIntExpr sumIntExpr_){
	_visitVarCreator(sumIntExpr_);
	super.visit(sumIntExpr_);
    }

    /** Visit a node of type {@link VariableExpr}
     *  and traverse its children depth first*/
    public void visit(VariableExpr variableExpr_){
	_names.add(variableExpr_.getVar().getId().nodeString());
        super.visit(variableExpr_);
    }

    /** Visit a node of type {@link Variable}
     *  and traverse its children depth first*/
    public void visit(Variable variable_){
	_names.add(variable_.getId().nodeString());
        super.visit((Node)variable_);
    }

    /** Visit a node of type {@link QualifiedName}
     *  and traverse its children depth first*/
    public void visit(QualifiedName qualifiedName_){
	_names.add(qualifiedName_.getId().nodeString());
	_names.add(qualifiedName_.nodeString());
        visit((Node)qualifiedName_);
    }

    /** Visit a node of type {@link Id}
     *  and traverse its children depth first*/
    public void visit(Id id_){
	_names.add(id_.nodeString());
        visit((Node)id_);
    }
}